import uuid
from Project.HTools.generate.generate_panel import GeneratePanel
import wx
import random
import string


class GenerateMain(GeneratePanel):
    def __init__(self, parent):
        GeneratePanel.__init__(self, parent)
        self.__init_view()

    def __init_view(self):
        self.edit_uuid_num.SetValidator(wx.DefaultValidator)
        self.edit_uuid_num.Bind(wx.EVT_CHAR, self.on_char)
        self.edit_pwd_num.Bind(wx.EVT_CHAR, self.on_char)
        pass

    def generate_uuid(self, event):
        try:
            result = []
            for i in range(int(self.edit_uuid_num.GetValue())):
                result.append(str(uuid.uuid4()))
            self.edit_uuid_result.SetValue("\n".join(result))
        except ValueError:
            self.__show_message('输入的生成条数有误')

    def on_char(self, event):
        key_code = event.GetKeyCode()
        if chr(key_code) in '0123456789':
            event.Skip()
        elif key_code in [wx.WXK_BACK, wx.WXK_DELETE, wx.WXK_LEFT, wx.WXK_RIGHT]:
            event.Skip()
        else:
            return

    def __show_message(self, message):
        wx.MessageDialog(self, message, '操作提醒', wx.OK).ShowModal()

    def buildComplexPwd(self, event):
        a = self.edit_pwd_num.GetValue()
        if a == '':
            self.__show_message('位数不能为空')
            return
        length = int(a)
        letters = string.ascii_letters + string.digits + string.punctuation
        result = ''.join(random.choice(letters) for i in range(length))
        self.edit_complex_pwd.SetValue(result)
